<?php

namespace App\Http\Controllers\Backend\Setting;

use App\Models\SettingLog;
use App\Models\SettingPopupSwitch;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SettingPopupSwitchController extends Controller
{
    use \App\Library\Traits\SettingLog;
    const SETTING_TABLE = 'setting_popup_switchs';

    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    /**
     * Description:扒图列表
     * Author: wjl
     * DateTime: 2019-04-10 18:26
     * @return mixed
     */
    public function index()
    {
        //列表
        $setting_popup = new SettingPopupSwitch();
        $popup_list = $setting_popup->select('channel')->get();
        $switch_list = SettingPopupSwitch::switchType();
        $all_channels = array_keys($switch_list);

        if ($popup_list) {
            $aready_channels = array_column($popup_list->toArray(), 'channel');

            $diff_channels = array_diff($all_channels, $aready_channels);

            if (sizeof($diff_channels) > 0) {
                $info = array();
                foreach ($diff_channels as $channel) {
                    $info[] = [
                        'channel' => $channel,
                        'desc' => $switch_list[$channel]['desc'],
                        'parent_id' => $switch_list[$channel]['parent_id'],
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ];
                }
                $setting_popup->insert($info);
            }
        } else {
            $info = array();
            foreach ($all_channels as $channel) {
                $info[] = [
                    'channel' => $channel,
                    'desc' => $switch_list[$channel]['desc'],
                    'parent_id' => $switch_list[$channel]['parent_id'],
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
            $setting_popup->insert($info);
        }

        $popup_list = $setting_popup->orderBy('id', 'desc')->get();

        if ($popup_list) {
            $info = [];
            foreach ($popup_list as $item) {
                $item->channel = $switch_list[$item->channel]['desc'];
                if ($item->parent_id > 0) {
                    $item->parent_id = $switch_list[$item->parent_id]['desc'];
                }
                $info[] = $item;
            }
            return success($info);
        }
        return success([]);
    }


    /**
     * Description:
     * Author: wjl
     * DateTime: 2019-04-12 09:35
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $setting_popup = new SettingPopupSwitch();
        $setting_popup->setConnection('backend');
        $info = $setting_popup->where('id', $id)->first();
        if ($info) {
            $switch_list = SettingPopupSwitch::switchType();
            $info->parent_id = $switch_list[$info->parent_id]['desc'];
            $info->channel = $switch_list[$info->channel]['desc'];
            return success($info);
        }
        return failed('当前信息不存在');
    }

    /**
     * Description:更新
     * Author: wjl
     * DateTime: 2019-04-12 09:34
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update(Request $request, $id)
    {
        $updateData = $request->only(['desc', 'status', 'url', 'title']);

        if (isset($updateData['title'])) {
            $updateData['title'] = trim($updateData['title']);
        }
        if (isset($updateData['url'])) {
            $updateData['url'] = trim($updateData['url']);
        }
        if (isset($updateData['desc'])) {
            $updateData['desc'] = trim($updateData['desc']);
        }
        if (isset($updateData['status'])) {
            $updateData['status'] = intval($updateData['status']);
        }

        $info = SettingPopupSwitch::where('id', $id)->update($updateData);

        if ($info) {

            $this->setting_log($this->user->id, self::SETTING_TABLE, $id, SettingLog::LOG_UPDATE, $updateData);
            return updated('');
        }
        return failed('更新失败');
    }

    /**
     * Description:
     * Author: wjl
     * DateTime: 2019-04-12 09:32
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public function destroy($id)
    {
        $result = SettingPopupSwitch::where('id', $id)->delete();

        if ($result) {
            $this->setting_log($this->user->id, self::SETTING_TABLE, $id, SettingLog::LOG_DELETE);
            return deleted();
        } else {
            return failed('删除失败');
        }
    }
}
